#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include"mainscene.h"
#include<QTimer>
#include<QLabel>
#include<QDebug>
#include<QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(520,788);

    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    this->setWindowTitle("选择关卡场景");

    QMenuBar*bar=menuBar();
    setMenuBar(bar);

    QMenu*startMenu= bar->addMenu("开始");

    QAction* quitACtion= startMenu->addAction("退出");

    connect(quitACtion,&QAction::triggered,[=](){
        this->close();
    });


    QSound* chooseSound=new QSound(":/res/TapButtonSound.wav",this);
    QSound* backSound=new QSound(":/res/BackButtonSound.wav",this);

    //返回按钮
    MyPushButton * backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){
        //告诉主场景我返回了 主场景监听返回按钮
        backSound->play();
        QTimer::singleShot(500,this,[=](){
        emit this->chooseSceneBack();
    });

    });

    //创建选择关卡的按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton * menuBtn=new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(100+i%4*90,210+i/4*90);

        connect(menuBtn,&MyPushButton::clicked,[=](){

            chooseSound->play();
            QString str=QString("您选择的是第%1关").arg(i+1);
            qDebug()<<str;

            //进入到游戏场景
            this->hide();
            play=new PlayScene(i+1);
            play->show();
            connect(play,&PlayScene::chooseSceneBack,[=](){
                this->show();
                delete play;
                play=NULL;
            });
        });

        QLabel*label=new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(100+i%4*90,210+i/4*90);
        //设置label上的文字对齐方式
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //设置让鼠标进行穿透 51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }

}
void ChooseLevelScene:: paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");

    painter.drawPixmap(this->width()-pix.width()*1.72,30,pix.width()*1.5,pix.height()*1.5,pix);
    


}
